<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
  
  <head>
    <meta charset="UTF-8">
    <title>欢迎页面-X-admin2.1</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <link rel="stylesheet" href="lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="css/public.css" media="all">

  </head>
  
  <body>
   
      <!--表格-->
      <table id="demo" lay-filter="test"></table>

    <!--toolbar :头部工具栏-->
    <!-- <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
      </div>
    </script> -->

    <!--tool :右侧工具栏-->
   <script type="text/html" id="barDemo">
	<a class="layui-btn layui-btn-xs" lay-event="look">查看</a>
      <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <!--开关-->
    <script type="text/html" id="switchTpl">
      <input type="checkbox" name="state" value="{{d.id}}" lay-skin="switch" lay-text="上架|下架" lay-filter="sexDemo" {{ d.state == 1 ? 'checked' : '' }}>
    </script>


    <script type="text/html" id="dateTpl">
        {{layui.util.toDateString(new Date(d.publishTime).getTime(), "yyyy-MM-dd HH:mm") }}
    </script>



<script src="lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="api/baseApi.js" charset="utf-8"></script>
<script th:inline="none">
      layui.use(['laydate','table','form'], function(){
        var laydate = layui.laydate;
        var table = layui.table;
        var form = layui.form;
				var $ = layui.$;
        
        //执行一个laydate实例
        laydate.render({
          elem: '#start' //指定元素
        });

        //执行一个laydate实例
        laydate.render({
          elem: '#end' //指定元素
        });
				
		
        //第一个实例
        table.render({
          elem: '#demo'
          ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
          ,defaultToolbar: ['filter', 'exports', 'print']//自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
          ,url: baseUrl + qsBaseUrl + 'pageQsList'	//数据接口
          ,page: true //开启分页
          ,limit: 10
          ,limits: [10,20,30]
          ,cols: [[ //表头
            {type: 'numbers', fixed: 'left' }
            ,{field: 'id', title: 'ID', width:80, sort: true, fixed: 'left'}
			,{field: 'nickname', title: '作者昵称', width:120 }
			,{field: 'name', title: '类型', width:80 }
			,{field: 'name', title: '类型', width:80 }
            ,{field: 'question', title: '文章标题', width:200}
			,{field: 'introduce', title: '简介', width:200 }
			,{field: 'readNum', title: '阅读', width:80 }
			,{field: 'answerNum', title: '回答', width:80 }
			,{field: 'collectNum', title: '收藏', width:80 }
            ,{field: 'publishTime', title: '发布时间', width:150 ,templet:'#dateTpl'}
			,{field: 'createTime', title: '创建时间', width:160 ,templet:'#dateTpl'}
            ,{field: 'state', title: '状态',templet: '#switchTpl', width:100 }
            ,{fixed: 'right', align:'center', toolbar: '#barDemo'}
          ]],
            text: {
                none: '暂无相关数据或权限不够'
            }
        });


        //头工具栏事件
        table.on('toolbar(test)', function(obj){
          var checkStatus = table.checkStatus(obj.config.id);
          switch(obj.event){
            case 'add':
              var data = checkStatus.data;
              //layer.alert(JSON.stringify(data));
              let index = layer.open({
                title:'添加问题',
                type:2,
                shade:0.2,
                maxmin:true,
                shadeClose:true,
                area:['100%','100%'],
                content:'qsAdd',
                yes: function(index, layero){
                  parent.location.reload(); // 父页面刷新
                  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                  parent.layer.close(index);
                }
              })
              break;
            case 'batchDel':
              var data = checkStatus.data;
              layer.msg('选中了：'+ data.length + ' 个');
              break;
                  //自定义头工具栏右侧图标 - 提示
            case 'LAYTABLE_TIPS':
              layer.alert('这是工具栏右侧自定义的一个图标按钮');
              break;
          };
        });

        //监听状态开关操作
        form.on('switch(sexDemo)', function(obj){
          layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
          var id = this.value; // 当行ID
          var state = this.checked ? '1' : '0'; // 选中状态
          var data ={id,state}
          console.log(data)
          $.ajax({
            url: baseUrl + qsBaseUrl ,
            type:'put',
            dataType: "json",
            contentType: "application/json;charset=UTF-8",
            data: JSON.stringify(data),
            success:function (resp) {
              layer.msg(resp.msg)
            },error(error){
                layer.msg(error.msg)
            }

          })
        });

        //监听行工具事件
        table.on('tool(test)', function(obj){
          var data = obj.data;
          //console.log(obj)
          if(obj.event === 'del'){
            layer.confirm('真的删除行么?', function(index){
              obj.del();
              $.ajax({
                url: baseUrl + questionBaseUrl + 'index/del/' +data.id,
                type:'get',
                success:function(res){
                  layer.msg("删除成功")
                  table.reload('demo')
                },
                error:function(res){
                  layer.msg("删除失败")
                }
              })
              layer.close(index);
            });

          }else if(obj.event === 'look'){
                //将数据存入本地sessionData中
                layui.sessionData('qsData',{
                  key:'qs'
                  ,value:data
                })

                let index = layer.open({
                  title:'问题查看',
                  type:2,
                  shade:0.2,
                  maxmin:true,
                  shadeClose:true,
                  area:['100%','100%'],
                  content:'qsLook',
                  yes: function(index, layero){
                    parent.location.reload(); // 父页面刷新
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                  }
                })
            }
        });



      });
    </script>
    <script>var _hmt = _hmt || []; (function() {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
      })();</script>
  </body>

</html>